<!-- 需求评估 -->

<template>

	<ListLayout>
		<Card>
			<template #head>
				评估结果
			</template>
			<template #body>
				<evan-form ref="form">
					<evan-form-item label="评估结果" required>
						<ItemRow :customActionSheetList="assessResultList" v-model="formData.assessResult"></ItemRow>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>

		<Card v-show="formData.assessResult == 1">
			<template #head>
				需求评估信息
			</template>
			<template #body>
				<evan-form :model="formDataPass" ref="formDataPass">
					<evan-form-item label="项目级别" prop="projectLevel">
						<ItemRow v-model="formDataPass.projectLevel" dicKey="itemProjectLevel"></ItemRow>
					</evan-form-item>
					<evan-form-item label="项目类型" prop="projectType">
						<ItemRow v-model="formDataPass.projectType" dicKey="projectType"></ItemRow>
					</evan-form-item>
					<evan-form-item label="项目负责人" prop="principal">
						<ItemRow ref="principal" v-model="formDataPass.principal" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="项目优先级" prop="priority">
						<ItemRow v-model="formDataPass.priority" dicKey="itemPriority"></ItemRow>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>


		<Card v-show="[2,3].includes(Number(formDataPass.projectLevel)) && formData.assessResult == 1">
			<template #head>
				其他信息
			</template>
			<template #body>
				<evan-form :model="formDataOther" ref="formDataOther">
					<evan-form-item label="业务答辩人" prop="businessRespondent">
						<ItemRow ref="businessRespondent" v-model="formDataOther.businessRespondent" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="技术答辩人">
						<ItemRow ref="technicalRespondent" v-model="formDataOther.technicalRespondent" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="其他参与人">
						<ItemRow ref="otherRespondent" v-model="formDataOther.otherRespondent" chooseType="select-user-multiple"></ItemRow>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>

		<Card v-show="formData.assessResult ==2">
			<template #head>
				不通过理由
			</template>
			<template #body>
				<evan-form :model="formDataNotPass" ref="formDataNotPass">
					<evan-form-item label="不通过理由" prop="failReason">
						<u-input v-model="formDataNotPass.failReason" type="textarea" input-align="right"></u-input>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>

		<Card v-show="formData.assessResult ==3">
			<template #head>
				继续调研理由
			</template>
			<template #body>
				<evan-form :model="formDataContinueReason" ref="formDataContinueReason">
					<evan-form-item label="继续调研理由" prop="continueReason">
						<u-input v-model="formDataContinueReason.continueReason" type="textarea" input-align="right"></u-input>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>

		<Card v-show="formData.assessResult ==4">
			<template #head>
				工单信息
			</template>
			<template #body>

				<evan-form :model="formDataTransferOrder" ref="formDataTransferOrder">
					<evan-form-item label="报修人" prop="user">
						<text>{{formDataTransferOrder.user}}</text>
					</evan-form-item>
					<evan-form-item label="报修人电话" prop="phone">
						<u-input input-align="right" v-model="formDataTransferOrder.phone"></u-input>
					</evan-form-item>
					<evan-form-item label="报修科室" prop="deptId">
						<ItemRow v-model="formDataTransferOrder.deptId" ref="deptId" chooseType="select-department"></ItemRow>
					</evan-form-item>


					<evan-form-item label="故障分类" prop="faultType">
						<ItemRow v-model="formDataTransferOrder.faultType" dicKey="faultClassification" chooseType="select-dic-cascader"></ItemRow>
					</evan-form-item>
					<evan-form-item label="故障描述" prop="faultRemark">
						<u-input input-align="right" type="textarea" v-model="formDataTransferOrder.faultRemark" placeholder="请填写故障描述" />
					</evan-form-item>
					<evan-form-item label="预计工时(小时)" prop="reqTime">
						<u-input input-align="right" v-model="formDataTransferOrder.reqTime" type="number"></u-input>
					</evan-form-item>

					<evan-form-item label="指派人员" prop="sendUser">
						<ItemRow v-model="formDataTransferOrder.sendUser" chooseType="select-user"></ItemRow>
					</evan-form-item>
					<evan-form-item label="紧急程度" prop="degree">

						<ItemRow v-model="formDataTransferOrder.degree" dicKey="gradeUrgency" chooseType="select-dic"></ItemRow>
					</evan-form-item>
					<evan-form-item label="影响范围" prop="rangeType">
						<ItemRow v-model="formDataTransferOrder.rangeType" dicKey="faultScope" chooseType="select-dic"></ItemRow>

					</evan-form-item>
					<evan-form-item label="报修时间" prop="createTime">
						<ItemRow v-model="formDataTransferOrder.createTime" ref="createTime" chooseType="date-picker-ymd"></ItemRow>
					</evan-form-item>
				</evan-form>
			</template>
		</Card>

		<template #footer>
			<HButtonGroup @leftClick="leftClick" @rightClick="rightClick">
				<template #left>
					取消
				</template>
				<template #right>
					提交
				</template>
			</HButtonGroup>
		</template>
	</ListLayout>

</template>
<script>
	import Card from '@/components/card/components/cardBaseWrapper.vue'
	import ListLayout from '@/components/layout/listLayout.vue'
	import HButtonGroup from '@/components/buttonGroup/groupTwo.vue'
	import ItemRow from '@/components/arrow/itemArrow.vue'
	import ProjectApi from '@/apis/project.js'
	import AttachmentCard from '@/components/card/attachment.vue'
	import PagesDemandRouterConfig from '@/pagesDemand/routerConfig.js'
	import { attachmentFormatter } from '@/common/submitFormatter.js'
	export default {
		components: {
			Card,
			ListLayout,
			HButtonGroup,
			ItemRow,
			AttachmentCard
		},

		data() {
			return {
				rulesFormDataPass: {
					principal: [{ message: '请选择项目负责人', required: true, }],
					projectLevel: [{ message: '请选择项目级别', required: true, }],
					priority: [{ message: '请选择项目优先级', required: true, }],
					projectType: [{ message: '请选择项目类型', required: true, }],

				},
				rulesFormDataOther: {
					businessRespondent: [{ message: '请选择业务答辩人', required: true }],
				},
				rulesFormDataNotPass: {
					failReason: [{ message: '请填写不通过原因', required: true, }],
				},
				rulesFormDataContinueReason: {
					continueReason: [{ message: '请填写继续调研原因', required: true, }],
				},
				rulesFormDataTransferOrder: {
					user: [{ message: '请填写报修人', required: true, }],
					phone: [{ message: '请填写报修人电话', required: true, }],
					faultType: [{ message: '请选择故障分类', required: true, }],

					degree: [{ message: '请选择紧急程度', required: true, }],
					rangeType: [{ message: '请选择影响范围', required: true, }],

					reqTime: [{ message: '请填写预计工时', required: true, }],
					sendUser: [{ message: '请选择指派人员', required: true, }],
					deptId: [{ message: '请选择报修科室', required: true, }],
					faultRemark: [{ message: '请填写故障描述', required: true, }],
				},
				formDataPass: {
					// 项目负责人
					principal: '',
					// 项目优先级
					priority: '',
					// 项目级别
					projectLevel: 1,
					// 项目类型
					projectType: 1,

				},

				formDataOther: {
					// 业务答辩人
					businessRespondent: '',
					// 技术答辩人
					technicalRespondent: '',
					// 其他答辩人
					otherRespondent: '',
				},
				formDataNotPass: {
					//  不通过原因
					failReason: '',
				},
				formDataContinueReason: {
					// 继续调研原因
					continueReason: '',
				},
				formDataTransferOrder: {
					// 报修人
					user: '',
					// 报修人电话
					phone: '',
					//  故障分类
					faultType: '',
					//  预计工时
					reqTime: '',
					// 指派人员
					sendUser: '',
					// 紧急程度
					degree: '',
					// 影响范围
					rangeType: '',
					// 报修时间
					createTime: '',
					// 报修科室
					deptId: null,
					// 故障描述
					faultRemark: '',
				},
				formData: {
					itemCode: '',
					// 评估结果，1-通过，2-不通过，3-继续调研，4-转工单
					assessResult: 1,
				},
				assessResultList: [{
					name: '通过',
					value: 1
				}, {
					name: '不通过',
					value: 2
				}, {
					name: '继续调研',
					value: 3
				}, {
					name: '转工单',
					value: 4
				}],

				routerParams: {
					itemCode: ''
				}

			}
		},
		onLoad(query) {
			let { itemCode } = query
			this.routerParams.itemCode = itemCode
			this.initData()
		},
		onReady() {
			this.$nextTick(() => {
				this.$refs.formDataPass.setRules(this.rulesFormDataPass)
				this.$refs.formDataOther.setRules(this.rulesFormDataOther)
				this.$refs.formDataNotPass.setRules(this.rulesFormDataNotPass)
				this.$refs.formDataContinueReason.setRules(this.rulesFormDataContinueReason)
				this.$refs.formDataTransferOrder.setRules(this.rulesFormDataTransferOrder)

			})
		},
		methods: {
			async initData() {

				let res = await ProjectApi.Demand.GetDemandDetail(this.routerParams.itemCode)
				const body = res.body || {}
				let { demand, firstGrading, demandAssess } = body

				const data = demandAssess || firstGrading;
				this.$set(this.formData, 'assessResult', data?.result || 1);
				this.$set(this.formDataPass, 'principal', data?.principal || '');
				this.$set(this.formDataPass, 'projectType', data?.projectType);
				this.$set(this.formDataPass, 'priority', data?.priority || '');
				this.$set(this.formDataPass, 'projectLevel', data?.projectLevel || '');


				this.$set(this.formDataOther, 'businessRespondent', data?.businessRespondent || '');
				this.$set(this.formDataOther, 'technicalRespondent', data?.technicalRespondent || '');
				this.$set(this.formDataOther, 'otherRespondent', data?.otherRespondent || '');
				this.$set(this.formDataNotPass, 'failReason', data?.failReason || '');
				this.$set(this.formDataContinueReason, 'continueReason', data?.continueReason || '');






				this.$set(this.formDataTransferOrder, 'user', demand.proposerList[0].sponsor);
				this.$set(this.formDataTransferOrder, 'phone', demand.proposerList[0].phone);
				this.$set(this.formDataTransferOrder, 'deptId', demand.proposerList[0].organizer);
				this.$set(this.formDataTransferOrder, 'createTime', demand.createTime);

				this.$set(this.formDataTransferOrder, 'faultRemark', demand.title);

				this.$refs.principal.resetInit(this.$options.filters.user(this.formDataPass.principal))
				this.$refs.businessRespondent.resetInit(this.$options.filters.user(this.formDataOther.businessRespondent))
				this.$refs.technicalRespondent.resetInit(this.$options.filters.user(this.formDataOther.technicalRespondent))
				this.$refs.otherRespondent.resetInit(this.$options.filters.users(this.formDataOther.otherRespondent))

				this.$refs.deptId.resetInit(this.$options.filters.subject(this.formDataTransferOrder.deptId))
				this.$refs.createTime.resetInit(this.$options.filters.date(this.formDataTransferOrder.createTime))

			},

			leftClick() {
				wx.navigateBack()
			},
			async rightClick() {
				let self = this;
				let is = false

				if (self.formData.assessResult == 1) {
					is = await self.$refs.formDataPass.validate()

					if ([2, 3].includes(Number(self.formDataPass.projectLevel))) {
						is = await self.$refs.formDataOther.validate()
					}


				} else if (self.formData.assessResult == 2) {
					is = await self.$refs.formDataNotPass.validate()
				} else if (self.formData.assessResult == 3) {
					is = await self.$refs.formDataContinueReason.validate()
				} else {
					is = await self.$refs.formDataTransferOrder.validate()
				}

				if (is) {
					self.formData.itemCode = self.routerParams.itemCode
					const params = {
						...self.formData,
						...self.formDataPass,
						...self.formDataOther,
						...self.formDataNotPass,
						...self.formDataContinueReason,
						transferOrderStr: JSON.stringify(self.formDataTransferOrder)
					};
					console.log(params);
					await ProjectApi.Demand.SubmitDemandAssess(params);
					wx.redirectTo({
						url: PagesDemandRouterConfig.Demand
					})
				}
			},

		}
	}
</script>
